<template>
  <a-spin :spinning="loading">
    <a-form v-bind="formItemLayout">
      <a-row>
        <a-col :span="24">
          <a-form-item label="参数" v-bind="validateInfos.body">
            <CodeEditor v-model:value="value" :mode="modeValue" />
	          <!-- <a-textarea v-model:value="formData.body" :rows="4" placeholder="请输入参数" :disabled="disabled"/> -->
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts">
  import { defineComponent, ref, reactive, toRaw } from 'vue';
  import { queryInterfacePostParamListByMainId } from '../InterfaceTest.api';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { Form } from 'ant-design-vue';
  import { CodeEditor } from '/@/components/CodeEditor';
  const useForm = Form.useForm;

  export default defineComponent({
    name: 'InterfacePostParamForm',
    components:{
      CodeEditor
    },
    props:{
      disabled:{
        type: Boolean,
        default: false
      }
    },
    setup(){
      const { createMessage } = useMessage();
      const isForm = true;
      const loading = ref(false);
      const formData = reactive<Record<string, any>>({
        id: '',
        body: '',   
      });
      //表单验证
      const validatorRules = reactive({
        body: [{ required: true, message: '请输入参数!'},],
      })
      const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, {immediate: false});
      const formItemLayout = {
        // labelCol: { xs: { span: 24 }, sm: { span: 5 } },
        // wrapperCol: { xs: { span: 24 }, sm: { span: 16 } },
        labelCol: {style: { width: '100px' }},
        wrapperCol: { },
      };

      async function initFormData(mainId) {
        resetFields();
        let tmpData = {}
        if(mainId){
          let list = await queryInterfacePostParamListByMainId(mainId);
          if(list && list.length>0){
            let temp = list[0];
            Object.keys(formData).forEach((key) => {
              if(temp.hasOwnProperty(key)){
                tmpData[key] = temp[key]
              }
            })
          }
        }
        //赋值
        Object.assign(formData,tmpData);
      }

      async function getFormData() {
        await validate();
        let subFormData = toRaw(formData);
        if(Object.keys(subFormData).length>0){
          return subFormData
        }
        return false;
      }

      function setFieldsValue(values) {
        if(values){
          Object.keys(values).map(k=>{
            formData[k] = values[k];
          });
        }
      }

      /**
       * 值改变事件触发-树控件回调
       * @param key
       * @param value
       */
      function handleFormChange(key, value) {
        formData[key] = value;
      }


      return {
        loading,
        formData,
        formItemLayout,
        initFormData,
        getFormData,
        setFieldsValue,
        handleFormChange,
        isForm,
        validateInfos
      }
    }
  });
</script>
